package generationTache.generationAleatoire;

import generationTache.IGenerationTache;

import java.util.Random;


/**
 * 
 * @author nicolas rault et matthieu allon
 * Permet de générer des taches periodiques aléatoirement.
 */
public class TachePeriodiqueAl implements IGenerationTache 
{
	
	private static int UpTotal = 0;
	private static int nbTacheTotal = 0;
	private int Up;
	private int Di;
	private int Ci;
	private int ri;
	private int Pi;
	private boolean active;
	private int duree;
	
	/**
	 * Constructeur de taches periodiques aleatoires.
	 * @param Up part du processeur alloue aux taches periodiques
	 * @param nbTache nombre total de taches periodiques souhaités
	 */
	public TachePeriodiqueAl(int Up)
	{
		if(UpTotal != 0)
		{
		}
		if(nbTacheTotal == 0){
			UpTotal = Up;
		}	
		
		active = false;
		duree = 0;
		
	}
	
	/**
	 * Permet de calculer les différentes variables liés aux taches périodiques
	 * @param nbTache nombre total de tache souhaitée
	 */
	public void calculParamTachePeriodAlea(int nbTache)
	{
		double repartition = UpTotal/(nbTache-nbTacheTotal);
		int nbAleatoire = 0;
		while (nbAleatoire == 0){
			Random r = new Random();
			nbAleatoire = (3*nbTache) + r.nextInt(10 - 0);
		}
		float Pi = nbAleatoire;
		float Di = Pi;
		nbAleatoire = 0;
		float cci = Float.MAX_VALUE;
		if(nbTache-nbTacheTotal != 1)
		{
			float tmp = cci/Pi;
			while ((tmp*100)>(repartition)){
				Random r = new Random();
				cci = 1+r.nextInt(10 - 0);
				tmp = cci/Pi;
			}
		}
		else
		{
			cci = (int)(repartition*Pi)/100;
		}
		
		float tmp = ((float)cci)/((float)Pi);
		UpTotal -= tmp*100;
		this.Ci = (int)cci;
		if(Ci == 0)
		{
			Ci++;
		}
		this.Pi = (int)Pi;
		this.Di = (int)Di;
		nbTacheTotal++;
	}
	
	public static void setUpTotal(int upTotal) {
		UpTotal = upTotal;
	}
	
	public static void setNbTacheTotal(int nbTacheTotal) {
		TachePeriodiqueAl.nbTacheTotal = nbTacheTotal;
	}
	
	public boolean getActive()
	{
		return active;
	}
	
	public void setActive(boolean active)
	{
		this.active = active;
	}
	
	public int getDuree()
	{
		return duree;
	}
	
	public void setDuree(int duree)
	{
		this.duree = duree;
	}

	public static int getUpTotal() {
		return UpTotal;
	}

	public static int getNbTacheTotal() {
		return nbTacheTotal;
	}

	public int getDi() {
		return Di;
	}

	public int getCi() {
		return Ci;
	}

	public int getRi() {
		return ri;
	}

	public int getPi() {
		return Pi;
	}
	
	public int getUp() {
		return Up;
	}
	
	public void setRi(int ri){
		this.ri = ri;
	}
	
	public void setPi(int Pi){
		this.Pi = Pi;
	}
	
	public void setDi(int Di){
		this.Di = Di;
	}
	
	public void setCi(int Ci){
		this.Ci = Ci;
	}

	public void setUp(int Up) {
		this.Up = Up;
	}
}